import z from 'zod'
import { request } from '..'
import { readingQuestionGroupDetailSchema } from './bank'

// 获取作业列表
const getHomeworkListRequestSchema = z.object({
  clazz: z.union([z.null(), z.string()]).optional(),
  pageNum: z.union([z.number(), z.null()]).optional(),
  pageSize: z.union([z.number(), z.null()]).optional(),
  search: z.union([z.null(), z.string()]).optional(),
  // 0表示未截止，1表示已截止
  status: z.union([z.number(), z.null()]).optional(),
})
export function getHomeworkList(params: z.infer<typeof getHomeworkListRequestSchema>) {
  return request({
    method: 'GET',
    url: '/homework/list',
    params,
    paramsValidator: getHomeworkListRequestSchema,
    responseValidator: z.object({
      entry: z.object({
        homeworks: z.array(z.object({
          allCount: z.string(),
          clazz: z.string(),
          completionCount: z.string(),
          ddl: z.string(),
          id: z.string(),
          publishTime: z.string(),
          status: z.number(),
          title: z.string(),
          classId: z.string(),
        })),
        total: z.union([z.number(), z.null()]),
      }),
    }),
  })
}

// 获取单个作业详情(包含多个小题)
const getHomeworkDetailRequestSchema = z.object({
  homeworkId: z.union([z.null(), z.string()]).optional(),
})
export function getHomeworkDetail(params: z.infer<typeof getHomeworkDetailRequestSchema>) {
  return request({
    method: 'GET',
    url: '/homework/detail',
    params,
    paramsValidator: getHomeworkDetailRequestSchema,
    responseValidator: z.object({
      entry: z.object({
        classId: z.union([z.null(), z.string()]).optional(),
        clazz: z.string(),
        deadline: z.string(),
        description: z.string(),
        homeworkId: z.string(),
        readingId: z.string(),
        title: z.string(),
      }),
    }),
  })
}

// 新增/修改一个作业
const createOrUpdateHomeworkRequestSchema = z.object({
  classId: z.string(),
  description: z.string(),
  deadline: z.string(),
  homeworkId: z.union([z.null(), z.string()]).optional(),
  title: z.string(),
  readingId: z.union([z.null(), z.string()]).optional(),
  readingDetails: readingQuestionGroupDetailSchema.optional(),
})
export type CreateOrUpdateHomework = z.infer<typeof createOrUpdateHomeworkRequestSchema>
export function createOrUpdateHomework(data: z.infer<typeof createOrUpdateHomeworkRequestSchema>) {
  return request({
    method: 'POST',
    url: '/homework/detail',
    data,
    dataValidator: createOrUpdateHomeworkRequestSchema,
    // responseValidator: z.object({
    //   homeworkId: z.string(),
    //   title: z.string(),
    //   description: z.string(),
    //   classId: z.string(),
    //   readingDetails: z.object({
    //     title: z.string(),
    //     description: z.string(),
    //     questions: z.array(z.object({
    //       questionTitle: z.string(),
    //       questionDescription: z.string(),
    //       correctAnswer: z.string(),
    //       options: z.array(z.object({
    //         label: z.string(),
    //         text: z.string(),
    //       })),
    //     })),
    //   }),
    // }),
  })
}

// 获取作业完成情况的列表
const getHomeworkCompletionListRequestSchema = z.object({
  classId: z.union([z.null(), z.string()]),
  homeworkId: z.union([z.null(), z.string()]),
  pageNum: z.union([z.number(), z.null()]),
  pageSize: z.union([z.number(), z.null()]),
  status: z.union([z.null(), z.string()]).optional(),
  studentName: z.union([z.null(), z.string()]).optional(),
  studentNo: z.union([z.null(), z.string()]).optional(),
})
export function getHomeworkCompletionList(params: z.infer<typeof getHomeworkCompletionListRequestSchema>) {
  return request({
    method: 'GET',
    url: '/homework/completion/list',
    params,
    paramsValidator: getHomeworkCompletionListRequestSchema,
    responseValidator: z.object({
      entry: z.object({
        allCount: z.number(),
        clazz: z.string(),
        completionCount: z.number(),
        ddl: z.string(),
        homeworkTitle: z.string(),
        publishTime: z.string(),
        status: z.number(),
        uncompletedCount: z.number(),
        total: z.number(),
        records: z.array(z.object({
          abilityLevel: z.union([z.number(), z.null()]).optional(),
          score: z.union([z.number(), z.null()]).optional(),
          status: z.union([z.number(), z.null()]).optional(),
          studentName: z.union([z.null(), z.string()]).optional(),
          studentNo: z.union([z.null(), z.string()]).optional(),
          submitTime: z.union([z.null(), z.string()]).optional(),
        })),
      }),
    }),
  })
}

// 获取单个作业的完成情况
const getHomeworkCompletionDetailRequestSchema = z.object({
  classId: z.union([z.null(), z.string()]).optional(),
  homeworkId: z.union([z.null(), z.string()]).optional(),
  studentId: z.union([z.null(), z.string()]).optional(),
})

export function getHomeworkCompletionDetail(params: z.infer<typeof getHomeworkCompletionDetailRequestSchema>) {
  return request({
    method: 'GET',
    url: '/homework/completion/detail',
    params,
    paramsValidator: getHomeworkCompletionDetailRequestSchema,
    responseValidator: z.object({
      abilityLevel: z.number(),
      completionDetails: z.array(z.object({
        answer: z.string(),
        description: z.string(),
        isCorrect: z.boolean(),
        options: z.array(z.object({
          optionLabel: z.string(),
          optionText: z.string(),
        })),
        score: z.number(),
        title: z.string(),
        type: z.number(),
      })),
      is_completed: z.boolean(),
      score: z.number(),
      studentName: z.string(),
      studentNo: z.string(),
      submitTime: z.string(),
      timeSpent: z.string(),
      title: z.string(),
    }),
  })
}

// 删除一个作业
const deleteHomeworkRequestSchema = z.object({
  classId: z.string(),
  homeworkId: z.string(),
})

export function deleteHomework(data: z.infer<typeof deleteHomeworkRequestSchema>) {
  return request({
    method: 'POST',
    url: '/homework/delete',
    data,
    dataValidator: deleteHomeworkRequestSchema,
  })
}
